package view.admin;

import controllers.RoomController;
import entity.Room;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import controllers.*;
import constants.*;
public class ManageRoom extends JPanel{
	
	JButton[] btStuffs;
	
	RoomList jpRoomList;
	RoomInfo jpRoomInfo;
	
	ArrayList<Room> 	rooms;
	ArrayList<JButton>	btRooms;
	ArrayList<entity.RoomType> roomTypes  = RoomController.getAllType();
	int currentRoom = -1;
	RoomListener roomListener = new RoomListener();
	
	public ManageRoom(){
		getRooms();
		setLayout(new BorderLayout());
		
		JPanel jpRoom = new JPanel(new BorderLayout());
		jpRoom.add(new RoomStuff(), BorderLayout.NORTH);
		jpRoom.add(jpRoomList = new RoomList(), BorderLayout.CENTER);
		jpRoom.add(jpRoomInfo = new RoomInfo(), BorderLayout.SOUTH);
		
		JPanel jpType = new JPanel(new FlowLayout());
		jpType.add(new ManageType());
		
		add(jpRoom, BorderLayout.CENTER);
		add(jpType, BorderLayout.EAST);
		
	}
	
	private void getRooms(){
		rooms = RoomController.getAll();
	}
	
	class RoomList extends JPanel{
		public RoomList(){
			setLayout(new FlowLayout(FlowLayout.LEFT));
			
			btRooms = new ArrayList<>();
			
			for (int i=0; i<rooms.size(); i++){
				JButton button = new JButton(rooms.get(i).getName());
				button.addActionListener(roomListener);
				btRooms.add(button);
				add(button);
			}
		}
		public void refresh(){
			removeAll();
			for (int i=0; i<btRooms.size(); i++)
				add(btRooms.get(i));
			repaint();
		}
	}	
	class RoomStuff extends JPanel{
		String[] stuffs = new String[]{
				"Thêm phòng", "Sửa phòng", "Xóa phòng"};
		public RoomStuff(){
			btStuffs = new JButton[stuffs.length];
			setLayout(new FlowLayout(FlowLayout.LEFT));
			setPreferredSize(new Dimension(0, 50));
			for (int i=0; i<btStuffs.length; i++){
				btStuffs[i] = new JButton(stuffs[i]);
				btStuffs[i].setPreferredSize(new Dimension(140, 30));
				btStuffs[i].addActionListener(new StuffListener());
				add(btStuffs[i]);
			}
		}
	}
	
	class RoomInfo extends JPanel{
		DefaultTableModel model;
		JTable table;
		public RoomInfo(){
			setPreferredSize(new Dimension(0, 55));
			initUI();
		}
		private void initUI(){
			setLayout(new BorderLayout());
			
			model = new DefaultTableModel();
			model.setDataVector(
					new String[][]{{"","","",""}}, 
					new String[]{"Mã phòng", "Tên phòng", "Loại phòng", "Tình trạng"});
			
			table = new JTable();
			table.setModel(model);
			table.setEnabled(false);
			
			DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
			centerRenderer.setHorizontalAlignment(JLabel.CENTER);
			table.getColumnModel().getColumn(0).setCellRenderer(centerRenderer);
			table.getColumnModel().getColumn(1).setCellRenderer(centerRenderer);
			table.getColumnModel().getColumn(2).setCellRenderer(centerRenderer);
			table.getColumnModel().getColumn(3).setCellRenderer(centerRenderer);
			table.setRowHeight(30);
			table.setAutoscrolls(false);
			
			add(new JScrollPane(table));
		}
		
		public void setRoom(int index){
                 	Room room = rooms.get(index);
			if (model.getRowCount() > 0)
				model.removeRow(0);
			model.addRow(new String[]{new Integer(room.getId()).toString(), room.getName(),
			roomTypes.get(room.getType()-1).getName(), 
					new Integer(room.getStatus()).toString()});
			table.setRowHeight(30);
		}
	}
	
	public void addRoom(){
		Room room = new Room();
		RoomOperate ro= new RoomOperate((JDialog)SwingUtilities.getRoot(this), 
						"Thêm phòng", 
						room,-1);
		if (ro.getResult()){
                        System.out.println("Thêm phòng "+ro.getRoom().getName());
			rooms.add(ro.getRoom());                       
			JButton button = new JButton(ro.getRoom().getName());
			button.addActionListener(roomListener);
			btRooms.add(button);
			jpRoomList.add(button);
			jpRoomList.validate();
		}
	}
	public void editRoom(){
		if (currentRoom < 0)
			return;
		Room room = rooms.get(currentRoom);
		boolean isOk = new RoomOperate((JDialog)SwingUtilities.getRoot(this), 
						"Sửa phòng", 
						room,room.getId())
						.getResult();
		if (isOk){
			btRooms.get(currentRoom).setText(rooms.get(currentRoom).getName());
			jpRoomList.validate();
		}
	}
	public void deleteRoom(){
		if (currentRoom < 0)
			return;
              if(rooms.get(currentRoom).getStatus()==Constants.EMPTY){  
		int choice = JOptionPane.showOptionDialog(null, 
				"Bạn có chắc muốn xóa không?", 
				"Xóa phòng", 
				JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, 
				null, 
				new String[]{"Xác nhận", "Bỏ qua"}, 0);
		if (choice == 0){
                            RoomController.remove(rooms.get(currentRoom).getId());
                            System.out.println("Xóa thành công phòng "+ rooms.get(currentRoom).getName()); 
                            rooms.remove(currentRoom);
                            btRooms.remove(currentRoom);
                            jpRoomList.refresh();
                            currentRoom --;
                  JOptionPane.showMessageDialog(null,"Xóa thành công");
		}
              }else{
                  JOptionPane.showMessageDialog(null,"Phòng không trống , không thể xóa");
              }
	}
	
	class RoomListener implements ActionListener{
		public void actionPerformed(ActionEvent event){
			for (int i=0; i<btRooms.size(); i++)
				if (btRooms.get(i) == event.getSource()){
					currentRoom = i;
					jpRoomInfo.setRoom(i);
					break;
				}
			
		}
	}
	class StuffListener implements ActionListener{
		public void actionPerformed(ActionEvent event){
			int i=0;
			for (i=0; i<btStuffs.length; i++)
				if (event.getSource() == btStuffs[i])
					break;
			switch(i){
				case 0:	addRoom();		break;
				case 1: editRoom();		break;	
				case 2: deleteRoom();	break;
			}
		}
	}
}
